
# encoding: utf-8
import re
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode
#from mobilephonehelper import operator_helper
from django.contrib.auth.models import User as AuthUser
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User,Permission

# Create your models here.
def ForbiddenUsernamesValidator(value):
    forbidden_usernames = ['admin', 'settings', 'news', 'about', 'help', 'signin', 'signup', 
        'signout', 'terms', 'privacy', 'cookie', 'new', 'login', 'logout', 'administrator', 
        'join', 'account', 'username', 'root', 'blog', 'user', 'users', 'billing', 'subscribe',
        'reviews', 'review', 'blog', 'blogs', 'edit', 'mail', 'email', 'home', 'job', 'jobs', 
        'contribute', 'newsletter', 'shop', 'profile', 'register', 'auth', 'authentication',
        'campaign', 'config', 'delete', 'remove', 'forum', 'forums', 'download', 'downloads', 
        'contact', 'blogs', 'feed', 'feeds', 'faq', 'intranet', 'log', 'registration', 'search', 
        'explore', 'rss', 'support', 'status', 'static', 'media', 'setting', 'css', 'js',]
    if value.lower() in forbidden_usernames:
        raise ValidationError('This is a reserved word.')
def ForbiddenMobilephoneNumberValidator(value):
    if not re.match('^[1]\d{10}$', value):
        raise ValidationError('This is a invalid mobile phone number.')
    

        
class  Department(models.Model):
    ''''department_type_array=(
                    (u'1',u'行政单位'),
                    (u'2',u'教学院(系部)'),
                    (u'3',u'学管单位'),)'''
    department_name=models.CharField(verbose_name=_(u'单位名称'),max_length=150)
  
    def __unicode__(self): 
        return smart_unicode(self.department_name)

    class Meta:
        verbose_name = _(u'单位信息')
        verbose_name_plural = _(u'单位信息')

class YudingPermission(models.Model):
    name=models.CharField(_(u'权限名称'), max_length=80)
    key=models.CharField(_(u'权限关键词'), max_length=250)

    class Meta:
        verbose_name = _(u'权限管理')
        verbose_name_plural = verbose_name
        
    def __unicode__(self): 
        return smart_unicode(self.name)


class UsersType(models.Model):
    name=models.CharField(_(u'人员类型名称'), max_length=80)
    permission=models.ManyToManyField(YudingPermission,verbose_name=u'组权限',null=True,blank=True)
    brief=models.TextField(_(u'简介'),null=True,blank=True)
    class Meta:
        verbose_name = _(u'人员类型')
        verbose_name_plural = _(u'人员类型') 
        
    def __unicode__(self): 
        return smart_unicode(self.name)


class Users(models.Model):
    user = models.OneToOneField(User,verbose_name=u'用户名')
    user_name=models.CharField(_(u'人员姓名'),max_length=30, validators=[ForbiddenUsernamesValidator])
    #user_type=models.ForeignKey(UsersType, verbose_name=u'人员类型')    #用户类型 包括 外聘人员
    user_type=models.ManyToManyField(UsersType,verbose_name=u'人员类型',null=True,blank=True)
    user_department=models.ForeignKey(Department, verbose_name=u'单位信息')
    user_mobilephone=models.CharField(_(u'手机号码'), max_length=25,null=True,blank=True)
    #user_employeeid=models.CharField(_(u'人员工号'), max_length=25)
    #user_mobilephone=models.CharField(_(u'手机号码'), max_length=25, validators=[ForbiddenMobilephoneNumberValidator])
    #user_mobilephoneoperator=models.CharField(_(u'运营商'),max_length=30,blank=True, null=True,editable=False)

    def __unicode__(self):
        return smart_unicode(self.user.username+' '+self.user_name)
        
    #def save(self, force_insert=False, force_update=False, using=None, 
    #   update_fields=None):
    #    self.user_mobilephoneoperator = operator_helper(self.user_mobilephone)
    #     return models.Model.save(self, force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)
    
    class Meta:
        verbose_name = _(u'人员信息')
        verbose_name_plural = _(u'人员信息')













